
package com.iplant.wms.controller.brm;

import com.iplant.base.controller.BaseController;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.wms.service.BRMService;
import com.iplant.wms.service.po.brm.BRMSerialNoRange;
import com.iplant.wms.service.po.brm.BRMSerialNoRule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/SerialNo")
public class SerialNoController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(SerialNoController.class);

    @Autowired
    BRMService wBRMService;

    @GetMapping("/RuleAll")
    Object RuleAll(HttpServletRequest request) {
        Object wResult = new Object();
        try {

            BMSEmployee wLoginUser = GetSession(request);
            String wName = StringUtils.parseString(request.getParameter("Name"));
            int wActive = StringUtils.parseInt(request.getParameter("Active"), -1);
            Pagination wPagination = GetPagination(request);
            ServiceResult<List<BRMSerialNoRule>> wServiceResult = wBRMService.BRM_QuerySerialNoRuleList(wLoginUser, wName, wActive, wPagination);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @GetMapping("/RuleInfo")
    Object RuleInfo(HttpServletRequest request) {
        Object wResult = new Object();
        try {

            BMSEmployee wLoginUser = GetSession(request);

            int wID = StringUtils.parseInt(request.getParameter("ID"));
            String wCode = StringUtils.parseString(request.getParameter("Code"));

            ServiceResult<BRMSerialNoRule> wServiceResult = wBRMService.BRM_QuerySerialNoRule(wLoginUser, wID, wCode);
            BRMSerialNoRule wServerRst = wServiceResult.Result;

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServerRst);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode(), null, wServerRst);
            }

        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @PostMapping("/RuleUpdate")
    Object RuleUpdate(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);
            int wUserID = wBMSEmployee.ID;
            String wUserName = wBMSEmployee.Name;

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            BRMSerialNoRule wBRMSerialNoRule = CloneTool.Clone(wParam.get("data"), BRMSerialNoRule.class);
            wBRMSerialNoRule.EditorName = wUserName;
            wBRMSerialNoRule.EditorID = wUserID;
            wBRMSerialNoRule.EditTime = Calendar.getInstance();
            if (wBRMSerialNoRule.ID <= 0) {

                wBRMSerialNoRule.CreatorName = wUserName;
                wBRMSerialNoRule.CreatorID = wUserID;
                wBRMSerialNoRule.CreateTime = Calendar.getInstance();

            }
            ServiceResult<Integer> wServiceResult = wBRMService.BRM_UpdateSerialNoRule(wBMSEmployee, wBRMSerialNoRule);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wBRMSerialNoRule);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @PostMapping("/RuleDelete")
    Object RuleDelete(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            BRMSerialNoRule wBRMSerialNoRule = CloneTool.Clone(wParam.get("data"), BRMSerialNoRule.class);

            ServiceResult<Integer> wServiceResult = wBRMService.BRM_DeleteSerialNoRule(wBMSEmployee, wBRMSerialNoRule);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @PostMapping("/RuleActive")
    Object RuleActive(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            int wActive = wParam.containsKey("Active") ? (int) wParam.get("Active") : 0;

            List<BRMSerialNoRule> wBRMSerialNoRuleList = CloneTool.CloneArray(wParam.get("data"), BRMSerialNoRule.class);
            ServiceResult<Integer> wServiceResult = new ServiceResult<Integer>(0);
            for (BRMSerialNoRule wBRMSerialNoRule : wBRMSerialNoRuleList) {

                wServiceResult = wBRMService.BRM_ActiveSerialNoRule(wBMSEmployee, wBRMSerialNoRule, wActive);

                if (!StringUtils.isEmpty(wServiceResult.getFaultCode()))
                    break;
            }

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }


    @GetMapping("/RangeAll")
    Object RangeAll(HttpServletRequest request) {
        Object wResult = new Object();
        try {

            BMSEmployee wLoginUser = GetSession(request);
            List<Integer> wCustomerIDList = StringUtils.parseIntList(request.getParameter("CustomerID"), ",");

            int wMaterialGroupID = StringUtils.parseInt(request.getParameter("MaterialGroupID"), -1);
            int wMaterialID = StringUtils.parseInt(request.getParameter("MaterialID"), -1);
            String wMaterialLike = StringUtils.parseString(request.getParameter("MaterialLike"));
            String wMaterialCode = StringUtils.parseString(request.getParameter("MaterialCode"));
            String wMaterialGroupFullName = StringUtils.parseString(request.getParameter("MaterialGroupFullName"));

            Pagination wPagination = GetPagination(request);

            ServiceResult<List<BRMSerialNoRange>> wServiceResult = wBRMService.BRM_QuerySerialNoRangeList(
                    wLoginUser,  wCustomerIDList, wMaterialGroupID, wMaterialGroupFullName, wMaterialID, wMaterialCode, wMaterialLike, wPagination);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }


    @PostMapping("/RangeUpdate")
    Object RangeUpdate(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            BRMSerialNoRange wBRMSerialNoRange = CloneTool.Clone(wParam.get("data"), BRMSerialNoRange.class);

            wBRMSerialNoRange.setUserInfo(wBMSEmployee);

            ServiceResult<Integer> wServiceResult = wBRMService.BRM_UpdateSerialNoRange(wBMSEmployee, wBRMSerialNoRange);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wBRMSerialNoRange);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }


    @PostMapping("/RangeDelete")
    Object RangeDelete(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            BRMSerialNoRange wBRMSerialNoRange = CloneTool.Clone(wParam.get("data"), BRMSerialNoRange.class);

            wBRMSerialNoRange.setUserInfo(wBMSEmployee);

            ServiceResult<Integer> wServiceResult = wBRMService.BRM_DeleteSerialNoRange(wBMSEmployee, wBRMSerialNoRange);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wBRMSerialNoRange);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }


}
